package cn.mg.framework.version;

import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;


/**
 * @author hezhaoming
 * @date 2020/5/13
 */
public class ApiVersionRequestMappingHandlerMapping extends
		RequestMappingHandlerMapping {

	@Override
	protected ApiVersionCondition getCustomTypeCondition(Class<?> handlerType) {
		ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
		return createCondition(apiVersion);
	}

	@Override
	protected ApiVersionCondition getCustomMethodCondition(Method method) {
		ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
		return createCondition(apiVersion);
	}

	private ApiVersionCondition createCondition(ApiVersion apiVersion) {
		return apiVersion == null || StringUtils.isEmpty(apiVersion.value()) ? null : new ApiVersionCondition(apiVersion.value());
	}
}
